/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.node.controler;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.asiainfo.ability.base.utils.ExceptionUtils;
import org.asiainfo.ability.node.service.NodeService;
import org.asiainfo.ability.node.utils.HttpClient;
import org.asiainfo.ability.node.vo.NodeOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: visen
 * @Date: 2018/1/16
 * @Description:
 */
@RestController
public class NodeControler {
    public static final Logger logger = LogManager.getLogger(NodeControler.class);

    @Autowired
    private NodeService nodeService;


    @RequestMapping(value = "/manage", method = RequestMethod.POST)
    public void manageNode(@RequestBody NodeOperation operation, HttpServletRequest request
            , HttpServletResponse response) {
        String error = nodeService.process(operation);
        if(error != null) {
            setRet(response, error);
        }
    }

    @RequestMapping(value = "/testActive", method = RequestMethod.GET)
    public void sleep(HttpServletRequest request, HttpServletResponse response) {
        setRet(response, "1");
    }


    private void setRet(HttpServletResponse response, String code) {
        try {
            response.getWriter().write(code);
        } catch (IOException e) {
            ExceptionUtils.logTrace(logger, e);
        }
    }


    @RequestMapping(value = "/deploy", method = RequestMethod.POST)
    public @ResponseBody
    void add(HttpServletRequest request, HttpServletResponse response) {
        MultipartFile jarFile = ((MultipartHttpServletRequest) request).getFile("file");
        String path = request.getParameter("path");
        try {
            path = NodeService.REPO  + path;

            File f = new File(path, jarFile.getOriginalFilename());
            f.setExecutable(true);
            FileUtils.copyInputStreamToFile(jarFile.getInputStream(),
                    f);
        } catch (IOException e) {
            setRet(response, e.getMessage());
            ExceptionUtils.logTrace(logger, e);
        }

    }

    public static void main(String[] args) throws Exception {
        Map param = new HashMap<>();
        param.put("path","abc");
        /*
        logger.error(HttpClient.uploadFile("http://127.0.0.1:19211/deploy"
                , new File("d:/a2.txt"),param));
*/

        HttpClient.uploadFile("http://127.0.0.1:19111/package/push"
                , new File("D:\\cloud\\esap\\ability-crm\\target\\ability-crm-1.0.jar"), null);

    }

}
